package components.asteroids;

import java.util.ArrayList;
import java.util.List;

import com.uqbar.vainilla.DeltaState;
import components.AsteroidCollComponent;
import components.CircularShape;
import components.Shape;
import components.SquareShape;
import components.weapon.Proyectile;

public class Asteroid extends AsteroidCollComponent {

	private int score;
	private double speed;
	private double dirVectY;
	private double dirVectX;
	
	private double fragmentsSpeed;
	
	private int hp;

	public Asteroid(double aSpeed, double xdir, double ydir, int radius,
			int aScore, int hitpoints, double x, double y) {
		super(radius*2, radius*2, x, y);
		this.score = aScore;
		this.speed = aSpeed;
		this.dirVectX = xdir;
		this.dirVectY = ydir;
		this.hp = hitpoints;
		this.fragmentsSpeed = aSpeed;

	}

	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	@Override
	public boolean isSolid() {
		return false;
	}


	@Override
	protected void effectiveUpdate(DeltaState deltaState) {
		this.generciMove(deltaState, this.getSpeed(), this.getDirVectX(),
				this.getDirVectY());
	}

	public synchronized void collideWithProyectile(Proyectile proyectile) {
		if (this.isEnable()){
			int power = proyectile.getPower();
			proyectile.impactOn(this);
			if (power >= this.getHp()){
				this.getScene().addAsteroids(this.getFragments());
				this.destroyAsteroid();
			}
			else{
				this.setHp(this.getHp() - power);
			}
		}
	}
	
	protected synchronized void destroyAsteroid(){
		if (this.isEnable()){
			this.setEnable(false);
			this.getScene().explosionOn(this.getX(), this.getY());
			this.destroyComponent();
			this.getScene().asteroidDestroyed(this);
		}
	}
	
	public List<Asteroid> getFragments(){
		return new ArrayList<Asteroid>();
	}

	// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public double getDirVectY() {
		return dirVectY;
	}

	public void setDirVectY(double dirVectY) {
		this.dirVectY = dirVectY;
	}

	public double getDirVectX() {
		return dirVectX;
	}

	public void setDirVectX(double dirVectX) {
		this.dirVectX = dirVectX;
	}

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public double getFragmentsSpeed() {
		return fragmentsSpeed;
	}

	public void setFragmentsSpeed(double fragmentsSpeed) {
		this.fragmentsSpeed = fragmentsSpeed;
	}

	@Override
	protected void onCollisionWithSolid(AsteroidCollComponent gc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void defineCollide(AsteroidCollComponent gc) {
		gc.collideWithAsteroid(this);
	}

	@Override
	public Shape defineShape() {
		return 	new CircularShape(this);
	}
}
